﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;

public partial class Admin_ManageGroups : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindgrid();
            displayAllUsers();
        }
    }

    private void displayAllUsers()
    {
        DataTable displayAllUsers = AdminCreateTestDAL.displayAllUsers();
        for (int i = 0; i < displayAllUsers.Rows.Count; i++)
        {
            ListItem li = new ListItem();
            li.Text = displayAllUsers.Rows[i][1].ToString();
            li.Value = displayAllUsers.Rows[i][0].ToString();
            chkUserList.Items.Add(li);
        }
        
    }

    private void clearAll()
    {
        txtGroupName.Text = "";
        txtDesc.Text = "";
        chkIsActive.Checked = false;
        lblMsg.Text = "";
        lblCountResult.Text = "";
        lblCount.Visible = false;
        lblCountResult.Visible = false;
        GVAvailGroups.SelectedIndex = -1;
        for (int i = 0; i < chkUserList.Items.Count; i++)
            if (chkUserList.Items[i].Selected == true)
                chkUserList.Items[i].Selected = false;
    }

    private void bindgrid()
    {
        DataTable DisplayAvailableGroups = AdminCreateTestDAL.DisplayAvailableGroups();
        DisplayAvailableGroups.Columns.Add("Is Active", typeof(System.String));
        for (int i = 0; i < DisplayAvailableGroups.Rows.Count; i++)
        {
            if (DisplayAvailableGroups.Rows[i][3].ToString() == "1")
            {
                DisplayAvailableGroups.Rows[i]["Is Active"] = "Yes";
            }
            else
            {
                DisplayAvailableGroups.Rows[i]["Is Active"] = "No";
            }

        }
        GVAvailGroups.DataSource = DisplayAvailableGroups;
        GVAvailGroups.DataBind();
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }

    protected void GVAvailGroups_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblCount.Visible = true;
        lblCountResult.Visible = true;

        int GroupID = Convert.ToInt32(GVAvailGroups.SelectedDataKey.Value);
        DataTable DisplayParticularGroup = AdminCreateTestDAL.DisplayParticularGroup(GroupID);
        txtGroupName.Text = DisplayParticularGroup.Rows[0][1].ToString();
        txtDesc.Text = DisplayParticularGroup.Rows[0][2].ToString();

        if (Convert.ToInt32(DisplayParticularGroup.Rows[0][3]) == 1)
        {
            chkIsActive.Checked = true;
        }
        else
        {
            chkIsActive.Checked = false;
        }

        DataTable DisplayUser = AdminCreateTestDAL.DisplayUserRelatedToGroup(GroupID);
        for (int i = 0; i < chkUserList.Items.Count; i++)
        {
            for (int j = 0; j < DisplayUser.Rows.Count; j++)
            {
                if (chkUserList.Items[i].Value.Trim() == DisplayUser.Rows[j][0].ToString().Trim())
                    chkUserList.Items[i].Selected = true;
            }
        }
        lblCountResult.Text = DisplayUser.Rows.Count.ToString();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int IsActive = -1;
        string userId="";

        for(int i=0;i<chkUserList.Items.Count;i++)
        {
            if(chkUserList.Items[i].Selected==true)
                userId+= chkUserList.Items[i].Value+"|";
        }

        if (chkIsActive.Checked == true)
        {
            IsActive = 1;
        }
        else
        {
            IsActive = 0;
        }

        if (GVAvailGroups.SelectedIndex == -1)
        {
            AdminCreateTestDAL.insertGroupInfo(txtGroupName.Text, txtDesc.Text, IsActive, userId);
            clearAll();
            lblMsg.Text = "Group Information Inserted successfully";
            bindgrid();
        }
        else
        {
            int GroupID = Convert.ToInt32(GVAvailGroups.SelectedDataKey.Value);
            AdminCreateTestDAL.UpdateGroupInfo(GroupID,txtGroupName.Text, txtDesc.Text, IsActive, userId);
            clearAll();
            lblMsg.Text = "Group Information Edited successfully";
            bindgrid();
        }
    }

    protected void txtGroupName_TextChanged(object sender, EventArgs e)
    {
        DataTable DisplayAvailableGroups = AdminCreateTestDAL.DisplayAvailableGroups();
        for(int i=0;i<DisplayAvailableGroups.Rows.Count;i++)
            if (txtGroupName.Text == DisplayAvailableGroups.Rows[i][1].ToString())
            {
                lblMsg.Text = "Sorry, Group Name alredy exists, Please Select different Group Name";
                clearAll();
                txtGroupName.Focus();
            }
    }
}